package com.chinasoft.controller;

import com.chinasoft.domain.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class BookController {

    //  http://localhost/showPageAndData1
    //原生request对象传递
    @RequestMapping("/showPageAndData1")
    public String showPageAndData1(HttpServletRequest request){
        request.setAttribute("name","lilei");
        return "page";
    }
    //使用Model形式参数
    @RequestMapping("/showPageAndData2")
    public String showPageAndData2(Model model){
        model.addAttribute("name","hmm");
        Book book = new Book();
        book.setName("spring入门案例");
        book.setPrice(66.0);
        model.addAttribute("book",book);
        return "page";
    }

    //使用ModelAndView形式参数
    @RequestMapping("/showPageAndData3")
    public ModelAndView showPageAndData3(ModelAndView modelAndView){
        Book book = new Book();
        book.setName("spring入门案例");
        book.setPrice(66.0);
        modelAndView.addObject("book",book);
        modelAndView.addObject("name","tom");

        modelAndView.setViewName("page");
        return modelAndView;
    }

    @RequestMapping("/showPageAndData4")
    public ModelAndView showPageAndData4(ModelAndView modelAndView){

        modelAndView.setViewName("forward:/WEB-INF/page/page.jsp");//该设定不会出发前后缀拼接格式
        return modelAndView;
    }

    @RequestMapping("/showPageAndData5")
    public ModelAndView showPageAndData5(ModelAndView modelAndView){

        modelAndView.setViewName("redirect:page.jsp");//该设定不会出发前后缀拼接格式
        return modelAndView;
    }
}